﻿using System;
using System.Data;
using System.Reflection;
using System.ServiceModel;

namespace RDIFramework.ServiceAdapter
{
    using RDIFramework.BusinessEntity;
    using RDIFramework.IService;
	using RDIFramework.Utilities;
	
	/// <summary>
    /// PermissionItemService
    /// 权限操作服务
	/// 
	/// 修改记录
	/// 
	///		2012-03-02 版本：1.0 EricHu 建立。
	///		
	/// 版本：1.0
	///
	/// <author>
	///		<name>EricHu</name>
	///		<date>2012-03-02</date>
	/// </author> 
	/// </summary>
	[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
	public class PermissionItemService : System.MarshalByRefObject, IPermissionItemService
	{
        private readonly string serviceName = RDIFrameworkMessage.PermissionItemService;

        /// <summary>
        /// .NET快速开发整合框架（RDIFramework.NET）数据库连接
        /// </summary>
        private readonly string RDIFrameworkDbConection = SystemInfo.RDIFrameworkDbConection;

        /// <summary>
        /// 添加操作权限项
        /// </summary>
        /// <param name="userInfo">当前用户</param>
        /// <param name="permissionEntity">权限定义实体</param>
        /// <param name="statusCode">状态码</param>
        /// <param name="statusMessage">状态信息</param>
        /// <returns>主键</returns>
        public string Add(UserInfo userInfo, PiPermissionItemEntity permissionItemEntity, out string statusCode, out string statusMessage)
        {
            statusCode = string.Empty;
            statusMessage = string.Empty;
            string returnValue = string.Empty;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    // 数据库事务开始
                    // dbProvider.BeginTransaction();
                    PiPermissionItemManager permissionItemManager = new PiPermissionItemManager(dbProvider, userInfo);
                    returnValue = permissionItemManager.Add(permissionItemEntity, out statusCode);
                    // 获得状态消息
                    statusMessage = permissionItemManager.GetStateMessage(statusCode);
                    // 写入日志信息
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.PermissionItemService_Add, MethodBase.GetCurrentMethod());
                    // 数据库事务提交
                    // dbProvider.CommitTransaction();
                }
                catch (Exception ex)
                {
                    // 数据库事务回滚
                    // dbProvider.RollbackTransaction();
                    // 记录异常信息
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }
            return returnValue;
        }

        /// <summary>
        /// 按详细信息添加
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="code">编号</param>
        /// <param name="fullName">名称</param>
        /// <param name="statusCode">状态码</param>
        /// <param name="statusMessage">状态信息</param>
        /// <returns>主键</returns>
        public string AddByDetail(UserInfo userInfo, string code, string fullName, out string statusCode, out string statusMessage)
        {
            statusCode = string.Empty;
            statusMessage = string.Empty;
            string returnValue = string.Empty;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiPermissionItemManager permissionItemManager = new PiPermissionItemManager(dbProvider, userInfo);
                    returnValue = permissionItemManager.AddByDetail(code, fullName, out statusCode);
                    // 获得状态消息
                    statusMessage = permissionItemManager.GetStateMessage(statusCode);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            return returnValue;
        }

        /// <summary>
        /// 获取权限项列表
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <returns>数据表</returns>
        public DataTable GetDT(UserInfo userInfo)
        {
            DataTable dataTable = new DataTable(PiPermissionItemTable.TableName);
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiPermissionItemManager permissionItemManager = new PiPermissionItemManager(dbProvider, userInfo);
                    dataTable = permissionItemManager.GetDT(PiPermissionItemTable.FieldDeleteMark, 0, PiPermissionItemTable.FieldSortCode);
                    dataTable.TableName = PiPermissionItemTable.TableName;
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.PermissionItemService_GetDT, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }
            return dataTable;
        }

        /// <summary>
        /// 按父节点获取列表
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="parentId">父节点主键</param>
        /// <returns>数据表</returns>
        public DataTable GetDTByParent(UserInfo userInfo, string parentId)
        {
            DataTable dataTable = new DataTable(PiPermissionItemTable.TableName);
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    // 获得组织机构列表
                    PiPermissionItemManager permissionItemManager = new PiPermissionItemManager(dbProvider, userInfo);
                    dataTable = permissionItemManager.GetDT(PiPermissionItemTable.FieldParentId, parentId, PiPermissionItemTable.FieldDeleteMark, 0, PiPermissionItemTable.FieldSortCode);
                    dataTable.TableName = PiPermissionItemTable.TableName;
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.PermissionItemService_GetDTByParent, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            return dataTable;
        }

        #region public DataTable GetDTByIds(UserInfo userInfo, string[] ids):按主键数组获取列表
        /// <summary>
        /// 按主键数组获取列表
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="ids">角色主键</param>
        /// <returns>数据表</returns>
        public DataTable GetDTByIds(UserInfo userInfo, string[] ids)
        {
            DataTable dataTable = new DataTable(PiRoleTable.TableName);
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiPermissionItemManager manager = new PiPermissionItemManager(dbProvider, userInfo);
                    dataTable = manager.GetDT(PiPermissionItemTable.FieldId, ids, PiPermissionItemTable.FieldSortCode);
                    dataTable.TableName = PiPermissionItemTable.TableName;
                    LogManager.Instance.Add(dbProvider, userInfo, serviceName, RDIFrameworkMessage.PermissionItemService_GetDTByIds, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }
            return dataTable;
        }
        #endregion

        /// <summary>
        /// 获取授权范围
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="userId">用户主键</param>
        /// <returns>数据表</returns>
        public DataTable GetLicensedDT(UserInfo userInfo, string userId)
        {
            DataTable dataTable = new DataTable(PiPermissionItemTable.TableName);
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiPermissionItemManager permissionAdminManager = new PiPermissionItemManager(dbProvider, userInfo);
                    string permissionItemId = permissionAdminManager.GetId(PiPermissionItemTable.FieldDeleteMark, 0, PiPermissionItemTable.FieldCode, "Resource.ManagePermission");
                    dataTable = permissionAdminManager.GetDTByUser(userId, permissionItemId);
                    dataTable.TableName = PiPermissionItemTable.TableName;
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.PermissionItemService_GetLicensedDT, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }
            return dataTable;
        }

        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="id">主键</param>
        /// <returns>实体</returns>
        public PiPermissionItemEntity GetEntity(UserInfo userInfo, string id)
        {
            PiPermissionItemEntity permissionItemEntity = new PiPermissionItemEntity();
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    // 创建实现类
                    PiPermissionItemManager permissionItemManager = new PiPermissionItemManager(dbProvider, userInfo);
                    DataTable returnDataTable = new DataTable(PiPermissionItemTable.TableName);
                    returnDataTable = permissionItemManager.GetDTById(id);
                    permissionItemEntity = new PiPermissionItemEntity(returnDataTable);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.PermissionItemService_GetEntity, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }
            return permissionItemEntity;
        }

        /// <summary>
        /// 按编号获取实体
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="code">编号</param>
        /// <returns>实体</returns>
        public PiPermissionItemEntity GetEntityByCode(UserInfo userInfo, string code)
        {

            PiPermissionItemEntity permissionItemEntity = null;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);

                    PiPermissionItemManager permissionItemManager = new PiPermissionItemManager(dbProvider, userInfo);
                    permissionItemEntity = new PiPermissionItemEntity(permissionItemManager.GetDTByCode(code));
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.PermissionItemService_GetEntityByCode, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            return permissionItemEntity;
        }

        /// <summary>
        /// 更新实体
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="permissionItemEntity">实体</param>
        /// <param name="statusCode">状态码</param>
        /// <param name="statusMessage">状态信息</param>
        /// <returns>影响行数</returns>
        public int Update(UserInfo userInfo, PiPermissionItemEntity permissionItemEntity, out string statusCode, out string statusMessage)
        {
            statusCode = string.Empty;
            statusMessage = string.Empty;
            int returnValue = 0;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiPermissionItemManager permissionItemManager = new PiPermissionItemManager(dbProvider, userInfo);
                    returnValue = permissionItemManager.Update(permissionItemEntity, out statusCode);
                    // 获得状态消息
                    statusMessage = permissionItemManager.GetStateMessage(statusCode);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.PermissionItemService_Update, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }
            return returnValue;
        }

        /// <summary>
        /// 移动实体
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="permissionItemId">权限项主键</param>
        /// <param name="parentId">父主键</param>
        /// <returns>影响行数</returns>
        public int MoveTo(UserInfo userInfo, string permissionItemId, string parentId)
        {
            int returnValue = 0;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiPermissionItemManager permissionItemManager = new PiPermissionItemManager(dbProvider, userInfo);
                    returnValue = permissionItemManager.MoveTo(permissionItemId, parentId);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.PermissionItemService_MoveTo, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }
            return returnValue;
        }

        /// <summary>
        /// 批量移动实体
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="permissionItemIds">权限项主键数组</param>
        /// <param name="parentId">父主键</param>
        /// <returns>影响行数</returns>
        public int BatchMoveTo(UserInfo userInfo, string[] permissionItemIds, string parentId)
        {

            int returnValue = 0;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiPermissionItemManager permissionItemManager = new PiPermissionItemManager(dbProvider, userInfo);
                    for (int i = 0; i < permissionItemIds.Length; i++)
                    {
                        returnValue += permissionItemManager.MoveTo(permissionItemIds[i], parentId);
                    }
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.PermissionItemService_BatchMoveTo, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }


            return returnValue;
        }

        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="id">主键</param>
        /// <returns>影响行数</returns>
        public int Delete(UserInfo userInfo, string id)
        {
            int returnValue = 0;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiPermissionItemManager permissionItemManager = new PiPermissionItemManager(dbProvider, userInfo);
                    returnValue = permissionItemManager.Delete(id);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.PermissionItemService_Delete, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }
            return returnValue;
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="ids">主键数组</param>
        /// <returns>影响行数</returns>
        public int BatchDelete(UserInfo userInfo, string[] ids)
        {
            int returnValue = 0;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiPermissionItemManager permissionItemManager = new PiPermissionItemManager(dbProvider, userInfo);
                    returnValue = permissionItemManager.Delete(ids);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.PermissionItemService_BatchDelete, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }
            return returnValue;
        }

        #region public int SetDeleted(UserInfo userInfo, string[] ids) 批量打删除标志
        /// <summary>
        /// 批量打删除标志
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="ids">主键数组</param>
        /// <returns>影响行数</returns>
        public int SetDeleted(UserInfo userInfo, string[] ids)
        {
            int returnValue = 0;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiPermissionItemManager permissionItemManager = new PiPermissionItemManager(dbProvider, userInfo);
                    returnValue = permissionItemManager.SetDeleted(ids);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.PermissionItemService_SetDeleted, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            return returnValue;
        }
        #endregion

        /// <summary>
        /// 批量保存
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="dataTable">数据表</param>
        /// <returns>影响行数</returns>
        public int BatchSave(UserInfo userInfo, DataTable dataTable)
        {
            int returnValue = 0;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    // 创建实现类
                    PiPermissionItemManager permissionAdminManager = new PiPermissionItemManager(dbProvider, userInfo);
                    returnValue = permissionAdminManager.BatchSave(dataTable);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.PermissionItemService_BatchSave, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            return returnValue;
        }

        /// <summary>
        /// 重新生成排序码
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="ids">主键数组</param>
        /// <returns>影响行数</returns>
        public int BatchSetSortCode(UserInfo userInfo, string[] ids)
        {
            int returnValue = 0;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiPermissionItemManager permissionItemManager = new PiPermissionItemManager(dbProvider, userInfo);
                    returnValue = permissionItemManager.BatchSetSortCode(ids);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.PermissionItemService_BatchSetSortCode, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }
            return returnValue;
        }

        /// <summary>
        /// 操作权限主健数组
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="moduleId">模块主键</param>
        /// <returns>主键数组</returns>
        public string[] GetIdsByModule(UserInfo userInfo, string moduleId)
        {
            string[] returnValue = null;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PermissionModuleManager modulePermissionManager = new PermissionModuleManager(dbProvider, userInfo);
                    returnValue = modulePermissionManager.GetPermissionIds(moduleId);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.PermissionItemService_GetIdsByModule, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }
            return returnValue;
        }
	}
}
